package com.dm.cloud.images.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.model.Network;
import com.dm.cloud.core.exception.CustomException;
import com.dm.cloud.images.common.po.ImagesEndpoint;
import com.dm.cloud.images.mode.NetworkCreatMode;
import com.dm.cloud.images.mode.NetworkDetail;
import com.dm.cloud.images.service.ImagesEndpointService;
import com.dm.cloud.images.service.NetworkService;
import com.dm.cloud.images.util.DockerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class NetworkServiceImpl implements NetworkService {
    @Autowired
    private ImagesEndpointService imagesEndpointService;
    @Override
    public List<NetworkDetail> list(String name,String nodeId) {
        List<ImagesEndpoint> endpoints=new ArrayList<>();
        if(StrUtil.isEmpty(nodeId)){
            endpoints = imagesEndpointService.list("state", 1);
        }else{
            ImagesEndpoint endpoint = imagesEndpointService.getById(nodeId);
            endpoints.add(endpoint);
        }

        List<NetworkDetail> networkDetails=new ArrayList<>();
        if(endpoints!=null){
            for (ImagesEndpoint endpoint : endpoints) {
                DockerClient client=null;
                try {
                    client = DockerUtil.get(endpoint.getHost(), endpoint.getDockerPort());
                    List<Network> networks = DockerUtil.listNetwork(client,name);
                    List<NetworkDetail> nodeDetail= NetworkDetail.buildByDockerNetworkList(networks,endpoint.getId(),endpoint.getName(),endpoint.getHost());
                    networkDetails.addAll(nodeDetail);
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    try {
                        client.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return networkDetails;
    }

    @Override
    public NetworkDetail create(NetworkCreatMode creatMode) {
        if(StrUtil.isEmpty(creatMode.getEndpointId())){
            throw new CustomException("请选择要部署的节点!");
        }
        ImagesEndpoint endPointPo = imagesEndpointService.getById(creatMode.getEndpointId());
        if(endPointPo==null || ObjectUtil.isEmpty(endPointPo)){
            throw new CustomException("节点不存在！");
        }
        DockerClient dockerClient = null;
        try {
            dockerClient = DockerUtil.get(endPointPo.getHost(), endPointPo.getDockerPort());
            Network network = DockerUtil.createnetwork(dockerClient, creatMode);
            return NetworkDetail.buildByDockerNetwork(network,endPointPo.getId(),endPointPo.getName(),endPointPo.getHost());
        }finally {
            try {
                dockerClient.close();
            } catch (IOException e) {
//                e.printStackTrace();
            }
        }
    }

    @Override
    public Boolean delete(String nodeId, String networkId) {

        if(StrUtil.isEmpty(networkId)){
            throw new CustomException("请选择要删除的网络!");
        }
        if(StrUtil.isEmpty(nodeId)){
            throw new CustomException("请选择要删除的节点!");
        }
        DockerClient dockerClient = null;
        try {
            dockerClient = imagesEndpointService.getClientByNodeId(nodeId);
            return DockerUtil.deleteNetwork(dockerClient,networkId);
        }catch (Exception e){
            throw new CustomException("网络删除失败!"+e.getMessage());
        }finally {
            try {
                dockerClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
